package com.shop.controller;

import com.shop.pojo.DtsStorage;
import com.shop.service.DtsStorageService;
import com.shop.util.ResponseUtil;
import com.shop.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

/**
 * @ClassName AdminStorageController
 * @Author xiao宋
 * @Date 16:03  2022/10/17
 * @Version 1.0
 * @Description TODO
 */
@RestController
@RequestMapping("/admin/storage")
public class AdminStorageController {
    @Autowired
    DtsStorageService storageService;

    @PostMapping("/delete")
    public Object delete(@RequestBody DtsStorage storage){
        storageService.delete(storage);
        return ResponseUtil.ok();
    }

    /**
     * 显示对象列表
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @param key
     * @param name
     * @return
     */
    @GetMapping("/list")
     public Object list(
     @RequestParam(value = "page",required = false, defaultValue = "1") Integer page,
     @RequestParam(value = "limit",required = false, defaultValue = "20")Integer limit ,
     @RequestParam(value = "sort",required = false, defaultValue = "add_time") String sort,
     @RequestParam(value = "order",required = false, defaultValue = "desc")String order,
     String key, String name
    ){
        Map<String,Object>data=storageService.findByPage(page,limit , sort, order, key, name);
        return ResponseUtil.ok(data);
    }
    @PostMapping("/create")
    public Object creat(MultipartFile file) throws Exception {
//        获取原始的时文件名
        String filename=file.getOriginalFilename();
        long size=file.getSize();
        String type=file.getContentType();
        String uuid= UUID.randomUUID().toString();
        String objectName=uuid+filename;
        DtsStorage storage = storageService.uploadFile(filename, (int) size, type, objectName, file.getBytes());
        return ResponseUtil.ok(storage);
    }
}
